/*
 * SymbolTable.java
 * Copyright (C) 5/21/2010, Lhrod
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 */
package Sindarin.Common;

import java.util.HashMap;
import Sindarin.ScriptParser.ScriptParserConstants;
import Sindarin.ScriptParser.Token;

/**
 *
 * @author Francisco J. Hernández
 */
public class SymbolTable implements java.io.Serializable {

    HashMap table = new HashMap();

    /** Creates a new instance of SymbolTable */
    public SymbolTable() {
    }

    protected SymbolTable(HashMap table) {
        this.table = table;
    }

    public boolean newSymbol(Symbol symbol) {
        table.put(symbol.token.image.toLowerCase(), symbol);
        return true;
    }

    public boolean newSymbol(Token token, int type) {
        String key = token.image;
        if (!exist(key.toLowerCase())) {
            Symbol nValue;
            switch (type) {
                case ScriptParserConstants.NUMBER:
                    nValue = new Symbol(null, ScriptParserConstants.NUMBER, token);
                    break;
                case ScriptParserConstants.STRING:
                    nValue = new Symbol(null, ScriptParserConstants.STRING, token);
                    break;
                case ScriptParserConstants.LOCATION:
                    nValue = new Symbol(null, ScriptParserConstants.LOCATION, token);
                    break;
                case ScriptParserConstants.MACROKEY:
                    nValue = new Symbol(null, ScriptParserConstants.MACROKEY, token);
                    break;
                case ScriptParserConstants.COLOR:
                    nValue = new Symbol(null, ScriptParserConstants.COLOR, token);
                    break;
                case ScriptParserConstants.UNDEF:
                    nValue = new Symbol(null, ScriptParserConstants.UNDEF, token);
                    break;
                case ScriptParserConstants.PARAM:
                    nValue = new Symbol(null, ScriptParserConstants.PARAM, token);
                    break;
                default:
                    return false;
            }
            table.put(key.toLowerCase(), nValue);
            return true;
        }
        return false;
    }

    public boolean exist(String key) {
        return table.containsKey(key.toLowerCase());
    }

    public Symbol find(String key) {
        return (Symbol) table.get(key.toLowerCase());
    }

    public boolean remove(String key) {
        key = key.toLowerCase();
        if (exist(key)) {
            table.remove(key);
            return true;
        }
        return false;
    }

    @Override
    public String toString() {
        String res = "Table: \n";
        Object[] tmp = table.keySet().toArray();
        for (int i = 0; i < tmp.length; i++) {
            res += i + ": " + this.find((String) tmp[i]) + "\n";
        }
        return res;
    }

    @Override
    public Object clone() {
        return new SymbolTable((HashMap) table.clone());
    }
}
